<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\User;

class HomeController extends Controller
{

    /**
    * 前台首页
    */
    public function index(Request $request) {

            $_SESSION['user_id'] = Auth::id();
            //导航 获取分类
            $cates = $this -> getCatesByDiGui(0);

            //今日推荐
            $today = DB::table('goods')->where('status','1')->where('hot','1')->orderBy('id','desc')->paginate(3);

            //获取商品
            // $goods = DB::table('goods')->orderBy('id','desc')->paginate(8);

        //友情链接
        $links = DB::table('links') -> get();
        
        if(Auth::check()){
            $id = Auth::user()->id;
            $info = DB::table('userdetail')->where('id','=',$id)->first();
    	   return view('home.index',['today'=>$today,'data'=>$request->all(),'cates'=>$cates,'links'=>$links,'info'=>$info]);
        } else {

    	   return view('home.index',['today'=>$today,'data'=>$request->all(),'cates'=>$cates,'links'=>$links]);
        }

    }


    /**
    * 递归方式来获取分类信息
    */
    public function getCatesByDiGui($pid)
    {
        //获取分类
        $cates = DB::table('cates')->where('pid','=', $pid)->get();
        //遍历分类信息
        $sub_cate = [];
        foreach($cates as $k=>$v) {
            $v->sub_cate = $this->getCatesByDiGui($v->id);
            $sub_cate[] = $v;
        }
        return $sub_cate;
    }


    /**
     * 数据处理,格式化显示信息
     */
    private function formatCateInfo($cates)
    {
        foreach($cates as $k=>$v){
            $arr = explode(',', $v->path);
            $level = count($arr) -1;

            $v->name = str_repeat('|-----',$level). $v->name;
        }
        return $cates;
    }


    /**
    * 获取类名
    */
    public static function cname($cid){       
        $c=DB::table('cates')->where('id','=',$cid)->first();      
        $cn=$c->name;
        return $cn;
    }


    /**
    * 前台商品列表
    */
     public function list(Request $request) {
        $cates = $this -> getCatesByDiGui(0);       

        //商品列表展示
        $cid=$request->input('cid');

        //栏目名称
        $cn=self::cname($cid);

        $n=DB::table('cates')->where('path','like',"%,{$cid}%")->orwhere('id',$cid)->lists('id');
        $goods=DB::table('goods')->wherein('cate',$n)->paginate(20);

        if(Auth::check()){
            $id = Auth::user()->id;
            $info = DB::table('userdetail')->where('id','=',$id)->first();
           return view('home.list',['goods'=>$goods,'cates'=>$cates,'cn'=>$cn,'info'=>$info]);
        } else {
    	   return view('home.list',['goods'=>$goods,'cates'=>$cates,'cn'=>$cn]);
        }
    }

    /**
    * 前台商品详情
    */
    public function details($id) {
        $cates = $this -> getCatesByDiGui(0);

        //商品详情
        $detail=DB::table('goods')->join('branks','goods.branks_id','=','branks.id')->where('goods.id','=',$id)->first();
        $sid = $detail->sid;
        //获取店铺
        $dianpu=DB::table('shops_details')->where('id','=',$sid)->first();

        //获取商品的颜色和尺码信息
        $attr = [];
        $attr['color'] = explode(',',$detail->color);
        $attr['shuxing'] = explode(',',$detail->shuxing);

        //商品图片
        $goods_imgs=DB::table('goods_imgs')->where('goods_id','=',$id)->get();

        //获取商品
        $goods = DB::table('goods')->orderBy('id','desc')->paginate(5);

         if(Auth::check()){
            $uid = Auth::user()->id;
            $info = DB::table('userdetail')->where('id','=',$uid)->first();
           return view('home.details',['detail'=>$detail,'dianpu'=>$dianpu,'goods_imgs'=>$goods_imgs,'attr'=>$attr,'cates'=>$cates,'goods'=>$goods,'info'=>$info,'id'=>$id]);
        } else {
           return view('home.details',['detail'=>$detail,'dianpu'=>$dianpu,'goods_imgs'=>$goods_imgs,'attr'=>$attr,'cates'=>$cates,'goods'=>$goods,'id'=>$id]);

        }

    }


    /**
    * 前台购物车页面
    */
    public function cart(Request $request)
    {

        // $goods= DB::table('goods')->whereIn('id')->get();

        //分配变量 解析模板
        if(Auth::check()){
            $uid = Auth::user()->id;
            $data = DB::table('cart')->join('goods','cart.goods_id','=','goods.id')->where('cart.users_id','=',$uid)->get();
             return view('home.cart',['data'=>$data]);
        }else{
            return redirect('/auth/login');
        }
    }

    /**
     * 删除购物车
     */
    public function cartDelete($id)
    {
        DB::table('cart')->where('cart_id','=',$id)->delete();
    }

    /**
     * 将购物车商品加入收藏
     */
    public function coll($id)
    {
        $uid = Auth::user()->id;
        $res = DB::table('goods_user')->where('user_id','=',$uid)->where('goods_id','=',$id)->first();
        if($res){
                
        }else{
            $data['user_id'] = $uid;
            $data['goods_id'] = $id;
            DB::table('goods_user')->insert($data);
        }
    }

    /**
     * 提交订单
     */
    public function order(Request $request)
    {
        $items = $request->items;
        if(!empty($items)){
            $uid = Auth::user()->id;
            $data = [];
            $canshu = [];
            $coupons = [];
            foreach ($items as $v) {
                $aaa = [];
                $res= DB::table('cart')->leftJoin('goods','cart.goods_id','=','goods.id')->where('cart_id','=',$v)->first();
                $user = User::find($uid);
                $coupons = $user->coupons()->where('coupons.sid','=',$res->sid)->get();
                $data[] = $res;
                $aaa['gid'] = $res->id;
                $aaa['num'] = $res->num;
                $canshu[] = $aaa;
            }
            $address = DB::table('address')->where('uid','=',$uid)->get();
            return view('home.pay',['data'=>$data,'address'=>$address,'canshu'=>$canshu,'coupons'=>$coupons]);
         }else{
            return back()->with('infos','<script>alert("请选择商品")</script>');
         }
    }

    /**
    * 前台结算页面
    */
    public function pay(Request $request)
    {
        if(Auth::check()){
            $canshu = [];
            $canshu[] = $request->all();
            $uid = Auth::user()->id; 
            $data = [];
            $coupons = [];
            foreach($canshu as $v){
                $res = DB::table('goods')->where('id','=',$v['gid'])->first();
                $user = User::find($uid);
                $coupons = $user->coupons()->where('coupons.sid','=',$res->sid)->get();
                $res->num = $v['num'];
                $data[] = $res;
            }
            $address = DB::table('address')->where('uid','=',$uid)->get();
            return view('home.pay',['data'=>$data,'address'=>$address,'canshu'=>$canshu,'coupons'=>$coupons]);
         }else{
            return redirect('/auth/login');
         }
    }


    /**
    * 结算成功页面
    */
    public function success(Request $request)

    {   
        $uid = Auth::user()->id;
        $address = DB::table('address')->where('uid','=',$uid)->where('status','=',0)->first();
            
        if(is_null($address)){
            return back()->with('info','<script>alert("请选择地址")</script>');
        }
        $cash = 0;
        $onum = '';
        $canshu = json_decode($request->canshu);
        $sid = '';
        $oid = '';
        foreach ($canshu as $v) {
                $data = [];
                $res = DB::table('goods')->where('id','=',$v->gid)->first();
                $res->num = $v->num;
                $data['uid'] = $uid;
                $data['oStatus'] = 1;
                $data['orderMoney'] = $v->num*$res->price;
                $data['counts'] = $v->num;
                $data['goods_id'] = $res->id;
                $data['shop_id'] = $res->sid;
                $sid = $res->sid;
                $data['onum'] = time().rand(1000000,9999999);
                $data['addr_id'] = $address->id;
                $data['sendTime'] = time();
                $onum = $data['onum'];
                $oid = DB::table('orders')->insertGetId($data);
                $cash+=$data['orderMoney'];
            }
            $user = User::find($uid);
            $coupons = $user->coupons()->where('coupons.sid','=',$sid)->first();
            if($coupons){
                 $cash = $cash - $coupons->money;
            }
            
        return view('home.success',['address'=>$address,'cash'=>$cash,'oid'=>$oid]);

    }

    

    /**
    * 前台用户的登陆页面
    */
    // public function login()
    // {
    //     return view('home.login');
    // }

    /**
    * 前台用户的注册页面
    */
    // public function register()
    // {
    //     return view('home.register');
    // }

    /**
     * 前台用户的注册操作
     */
    // public function doRegister(Request $request)
    // {
        
    // }



}
